package com.ruoyi.client.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.client.domain.entity.BlogUser;
import com.ruoyi.client.service.IBlogUserService;
import com.ruoyi.client.service.LoginService;
import com.ruoyi.common.core.constant.ClientConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.bo.LoginBody;
import com.ruoyi.common.core.domain.bo.LoginUser;
import com.ruoyi.common.satoken.utils.LoginHelper;
import com.ruoyi.common.web.core.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/client/user")
public class UserLoginController extends BaseController {
    @Autowired
    LoginService loginService;
    @Autowired
    IBlogUserService userService;

    /**
     * 登陆
     * @param loginBody
     * @return
     */
    @SaIgnore
    @PostMapping("login")
    public R<Map<String,Object>> Login(@RequestBody @Validated(ClientConstants.class) LoginBody loginBody) {
        String token = LoginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getType());
        return R.ok(Map.of("token",token));
    }
    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public R<Map<String, Object>> getInfo() {
        Long id = Long.valueOf((String) StpUtil.getLoginId());
//        BlogUser user = userService.getInfo(id);
        BlogUser user = userService.getById(id);
        return R.ok(Map.of("user",user));
    }

    /**
     * 退出登录
     */
    @SaIgnore
    @PostMapping("/logout")
    public R<Void> logout() {
        loginService.logout();
        return R.ok("退出成功");
    }
}
